<?php
class Category_model extends MY_Model {
    protected $table = 'tbl_category';
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function get_info($col, $col_val , $get_col)
            
    {
        $cate = $this->get_one($this->table,$col,$col_val);
       
        if($cate)
        {
            foreach ($cate as $val)
            {
                    return $val[$get_col];
                
            }
        }
    }
    public function is_parent($cate_id = '')
    {
        $this->db->select();
        $this->db->from($this->table);
        $this->db->where('cate_parent', $cate_id);
        $query = $this->db->get();
        if($query->num_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function show_cate($cate_parent = 0, $space = "", $trees = array())
    {
        $this->db->select();
        $this->db->from($this->table);
        $this->db->where('cate_parent', $cate_parent);
        $this->db->order_by('order', 'ASC');
        $query = $this->db->get();
        if(!$trees)
        {
            $trees = array();
        }
        if($query->num_rows() > 0)
        {
            $list_child = $query->result_array(); 
            foreach ($list_child as $child)
            {
                $parent_name = $this->get_info('cate_id', $child['cate_parent'], 'cate_name');
                $trees[] = array(   
                                'cate_id' => $child['cate_id'],
                                'cate_name'=>$space.$child['cate_name'],
                                'cate_parent'=>$child['cate_parent'],
                                'order'=>$child['order'],
                                'parent_name' => $parent_name
                                );
                if($this->is_parent($child['cate_id']))
                {
                    $trees = $this->show_cate($child['cate_id'], $space.'-&nbsp;', $trees);
                }
            }
        }
        return $trees;
    }
    public function childs($cate_parent = '')
    {
        $this->db->select();
        $this->db->from($this->table);
        $this->db->where('cate_parent', $cate_parent);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function move($order, $order_move)
    {
        $cate_id = $this->get_info('order', $order, 'cate_id');
        $sql = "UPDATE {$this->table} 
                SET `order` = {$order_move}
                WHERE `cate_id` = {$cate_id}
                ";
        
        
        $cate_id_move = $this->get_info('order', $order_move, 'cate_id');
        $sql_move = "UPDATE {$this->table} 
                SET `order` = {$order}
                WHERE `cate_id` = {$cate_id_move}
                ";
        $this->db->query($sql);
        $this->db->query($sql_move);
        
    }
    
}

